﻿using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Ketoan
{
	public class frmUser : System.Windows.Forms.Form
	{
		private LibKetoan.AccessData kt;
		private string i_userid="",s_userid="",s_pass="",s_right="";
		private DataSet dsr=new DataSet();
		private System.Windows.Forms.TextBox password;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox userid;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox hoten;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button butCancel;
		private System.Windows.Forms.Button butOk;
		private System.Windows.Forms.CheckBox chkAdmin;		
		private System.ComponentModel.Container components = null;

		public frmUser(LibKetoan.AccessData acc,DataSet dss,string m_userid,string m_user,string m_pass,string m_right,bool bEdit)
		{			
			InitializeComponent();
			kt=acc;
			dsr=dss;
			i_userid=m_userid;
			s_userid=m_user;
			s_pass=m_pass;
			s_right=m_right;
			hoten.Enabled=userid.Enabled=chkAdmin.Enabled=bEdit;
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmUser));
			this.password = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.userid = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.hoten = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.butCancel = new System.Windows.Forms.Button();
			this.butOk = new System.Windows.Forms.Button();
			this.chkAdmin = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// password
			// 
			this.password.BackColor = System.Drawing.SystemColors.HighlightText;
			this.password.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.password.Location = new System.Drawing.Point(64, 56);
			this.password.MaxLength = 10;
			this.password.Name = "password";
			this.password.PasswordChar = '*';
			this.password.Size = new System.Drawing.Size(197, 23);
			this.password.TabIndex = 2;
			this.password.Text = "";
			this.password.KeyDown += new System.Windows.Forms.KeyEventHandler(this.password_KeyDown);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(0, 56);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 23);
			this.label3.TabIndex = 10;
			this.label3.Text = "Password :";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// userid
			// 
			this.userid.BackColor = System.Drawing.SystemColors.HighlightText;
			this.userid.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.userid.Location = new System.Drawing.Point(64, 32);
			this.userid.MaxLength = 10;
			this.userid.Name = "userid";
			this.userid.Size = new System.Drawing.Size(197, 23);
			this.userid.TabIndex = 1;
			this.userid.Text = "";
			this.userid.KeyDown += new System.Windows.Forms.KeyEventHandler(this.userid_KeyDown);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(0, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 23);
			this.label2.TabIndex = 8;
			this.label2.Text = "Tên Login :";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// hoten
			// 
			this.hoten.BackColor = System.Drawing.SystemColors.HighlightText;
			this.hoten.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.hoten.Location = new System.Drawing.Point(64, 8);
			this.hoten.MaxLength = 50;
			this.hoten.Name = "hoten";
			this.hoten.Size = new System.Drawing.Size(197, 23);
			this.hoten.TabIndex = 0;
			this.hoten.Text = "";
			this.hoten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.hoten_KeyDown);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(0, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 23);
			this.label1.TabIndex = 6;
			this.label1.Text = "Họ tên :";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// butCancel
			// 
			this.butCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.butCancel.Image = ((System.Drawing.Bitmap)(resources.GetObject("butCancel.Image")));
			this.butCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butCancel.Location = new System.Drawing.Point(142, 112);
			this.butCancel.Name = "butCancel";
			this.butCancel.Size = new System.Drawing.Size(68, 25);
			this.butCancel.TabIndex = 4;
			this.butCancel.Text = " &Kết thúc";
			this.butCancel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butCancel.Click += new System.EventHandler(this.butCancel_Click);
			// 
			// butOk
			// 
			this.butOk.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.butOk.Image = ((System.Drawing.Bitmap)(resources.GetObject("butOk.Image")));
			this.butOk.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butOk.Location = new System.Drawing.Point(71, 112);
			this.butOk.Name = "butOk";
			this.butOk.Size = new System.Drawing.Size(68, 25);
			this.butOk.TabIndex = 3;
			this.butOk.Text = "    &Thêm";
			this.butOk.Click += new System.EventHandler(this.butOk_Click);
			// 
			// chkAdmin
			// 
			this.chkAdmin.Location = new System.Drawing.Point(64, 88);
			this.chkAdmin.Name = "chkAdmin";
			this.chkAdmin.Size = new System.Drawing.Size(72, 16);
			this.chkAdmin.TabIndex = 11;
			this.chkAdmin.Text = "Admin";
			// 
			// frmUser
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(280, 146);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.chkAdmin,
																		  this.butCancel,
																		  this.butOk,
																		  this.password,
																		  this.label3,
																		  this.userid,
																		  this.label2,
																		  this.hoten,
																		  this.label1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "frmUser";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Thêm người dùng";
			this.Load += new System.EventHandler(this.frmUser_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void butCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void butOk_Click(object sender, System.EventArgs e)
		{
			if (hoten.Text=="")
			{
				MessageBox.Show("Yều cầu nhập họ tên !",kt.Msg);
				hoten.Focus();
				return;
			}
			if (userid.Text=="")
			{
				MessageBox.Show("Yều cầu nhập tên login !",kt.Msg);
				userid.Focus();
				return;
			}
			kt.upd_dlogin(int.Parse(i_userid),hoten.Text.Trim(),userid.Text.Trim(),password.Text.Trim(),s_right,"","","","","",(chkAdmin.Checked)?1:0);
			kt.updrec(dsr.Tables[0],int.Parse(i_userid),userid.Text,password.Text,hoten.Text,s_right);
			this.Close();
		}
		public DataSet dsright
		{
			get
			{
				return dsr;
			}
		}
		private void hoten_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void userid_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void password_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void frmUser_Load(object sender, System.EventArgs e)
		{
			if(i_userid!="")
			{
				this.Text = "Sửa Password";
				foreach (DataRow r in kt.get_data("select * from " + kt.user + ".a_dlogin where id=" + i_userid).Tables[0].Rows)
				{
					hoten.Text = r["hoten"].ToString();
					userid.Text = r["userid"].ToString();
					password.Text = r["password_"].ToString();
					chkAdmin.Checked=r["admin"].ToString()=="1";		
				}
			}
			else
			{
				i_userid = kt.get_capid(-30).ToString();
			}
		}
	}
}
